package com.thp.bigdata.wcdemo;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

/**
 * 自定义的Combiner
 * @author tommy
 *
 */
public class MyCombiner extends Reducer<Text, IntWritable, Text, IntWritable>{
	@Override
	protected void reduce(Text key, Iterable<IntWritable> values,Context context) throws IOException, InterruptedException {
		//
		System.out.println("Combiner输入分组<" + key.toString() + ",N(N>=1)>");
		
		int count = 0;
		for(IntWritable value : values) {
			count += value.get();  // 这个count 最后就是某一个单词的汇总的值
			// 显示次数表示输入的k2,v2的键值对数量
			System.out.println("Combiner输入键值对<" + key.toString() + "," + value.get() + ">");
		}
		context.write(key, new IntWritable(count));
		System.out.println("Combiner输出键值对<" + key.toString() + "," + count+ ">");
	}
	
}
